package com.email.box.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.Map;

import static com.email.box.utils.ConvertUtil.getObj2Map;

/**
 * 自定义 Wrapper 工具类
 */
public class WrapperUtil {

    public static <T> QueryWrapper<T> allEqWrapper(Object object, QueryWrapper<T> queryWrapper) {
        Map<String, Object> map = getObj2Map(object);
        return queryWrapper.allEq(map);
    }

    public static <T> UpdateWrapper<T> allEqWrapper(Object object, UpdateWrapper<T> updateWrapper) {
        Map<String, Object> map = getObj2Map(object);
        return updateWrapper.allEq(map);
    }

    public static <T> QueryWrapper<T> allEqWrapper(Object object) {
        return allEqWrapper(object, new QueryWrapper<>());
    }

    public static <T> QueryWrapper<T> allLikeWrapper(Map<String, Object> params, QueryWrapper<T> queryWrapper) {
        if (CollectionUtils.isNotEmpty(params)) {
            params.forEach((k, v) -> {
                if (StringUtils.checkValNotNull(v)) {
                    queryWrapper.like(k, v);
                }
            });
        }
        return queryWrapper;
    }

    public static <T> QueryWrapper<T> allLikeWrapper(Object object, QueryWrapper<T> queryWrapper) {
        return allLikeWrapper(getObj2Map(object), queryWrapper);
    }

    public static <T> QueryWrapper<T> allLikeWrapper(Object object) {
        return allLikeWrapper(object, new QueryWrapper<>());
    }

}
